package com.qys.livesMall.finance.util;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.qys.livesMall.cache.RedisCache;
import com.qys.livesMall.cache.utils.CacheUtil;
import com.qys.livesMall.common.utils.SpringUtils;
import com.qys.livesMall.finance.common.FinanceRidesKeyPrefix;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;


import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @menu BusinessNoUtil
 * @date 2023/06/07 14:29
 */
public class BusinessNoUtil {

    /**
     *
     * @param businessType
     * @return
     */
    public static String get(AccountBusinessTypeEnum businessType){
        RedisCache redis=SpringUtils.getBean(RedisCache.class);
        //获取最后记录的时间
        String timeKey=CacheUtil.getKey(FinanceRidesKeyPrefix.BUSINESS_NO_LAST_TIME);
        Long nowTime= new Date().getTime();
        Long lastDate=redis.getCacheObject(timeKey);
        if (lastDate!=null&&lastDate > nowTime) {
            nowTime=lastDate;
        }else {
            redis.setCacheObject(timeKey,nowTime);
        }
        String noPrefix=businessType.getPrefix()+nowTime;
        String key=CacheUtil.getKey(FinanceRidesKeyPrefix.BUSINESS_NO_INCR,noPrefix);
        Long index=redis.incr(key,1);
        redis.expire(key,2, TimeUnit.MINUTES);
        String indexStr=String.format("%02d",index);
        return noPrefix+indexStr;
    }



}

